/* file that stratifies helper results by kid genderat event time */
/* Used for figure 5 and also figure b4 panel 1 */


clear
clear mata
clear matrix
set maxvar 30000
estimates clear

/* Load kid-wave file */
use data/constructed_data/kid_wave_all.dta if oldest_age>=50 & oldest_age<=85 & wave>=2, clear

//Set Panel
xtset kidid_group wave
tab wave

//Prepare variables
// Helping
gen khlp = cond(khelpr == 1, 1, 0)
replace khlp = . if khelpr == . // .z means not in helper file (so not helper) but . means no response

//Wealth Shocks
gen wpos = (real_hatota > 1.5*l.real_hatota) & l.real_hatota>20000 & l.real_hatota!=.
gen wneg = (real_hatota < .75*l.real_hatota) & l.real_hatota>20000 & l.real_hatota!=.

gen wneg2 = wneg
replace wneg2 = 0 if l.wpos==1
replace wneg2 = 0 if l2.wpos==1

//Life Expectancy
replace rliv75 = rliv85 if rliv75==.
replace rliv75 = rliv10 if rliv75==.

//Age and Race
egen ageXwave = group(oldest_age wave)
gen oldest_age2 = oldest_age^2
drop if any_nonwhite==.  /*113,781 household-waves, 19,384 households*/

// Weight
bysort hhidpn: egen weight = mean(rwthh)

/* rename some long names */
rename any_real_ktcamt at_pk // any transfer parent -> kid 
rename any_real_kfcamt at_kp // any transfer kid -> parent
rename any_rwidowed any_rw // any parent death?

/* helpr variable is khelpr */

/* file paths */
loc regsave_path output/event_study_regsave
di "Regsave Path: `regsave_path'"
foreach event in any_rhosp any_disab any_health3to5 any_rw wneg2 any_rlost_job  any_rcancr any_rstrok any_rcardiac any_rmemory {
    di "`event'"
    preserve
    xtset kidid_group wave
    //Setup
    gen temp = 1 if `event'==1 & l.`event'==0 & l2.`event'==0 
    bys kidid_group: egen firstevent = min(wave*temp)
    drop temp
    gen eventtime = wave - firstevent 
    gen eventever = firstevent~=.
    keep if eventever==1  

    //Event indicators
    gen b3m = eventtime<=-3
    gen b2 = eventtime==-2
    gen b1 = eventtime==-1
    gen e0 = eventtime==0
    forvalues l = 1/9 {
    gen a`l' = eventtime==`l'
    }

    //Regressions - Helper stratify by kid gender (alternative is to interact and plot the interaction)
	foreach lhs of varlist at_pk at_kp khlp {
		di "`lhs'"
		/* Female kids kgender == 2 */
		xtreg `lhs' b3m b2 e0 a1 a2 a3 a4 a5 a6 a7 a8 a9 oldest_age oldest_age2 i.wave [pweight = weight] if kgender == 2, fe cluster(hhidpn)
        eststo p`lhs'_`event'_fkids
		regsave b3m b2 e0 a1 a2 a3  using `regsave_path'/`event'_`lhs'_fkids.dta, replace ci addlabel(group, fkids)

		/* Male kids kgender == 1 */
		xtreg `lhs' b3m b2 e0 a1 a2 a3 a4 a5 a6 a7 a8 a9 oldest_age oldest_age2 i.wave [pweight = weight] if kgender == 1, fe cluster(hhidpn)
		eststo p`lhs'_`event'_mkids
        regsave b3m b2 e0 a1 a2 a3  using `regsave_path'/`event'_`lhs'_mkids.dta, replace ci addlabel(group, mkids)
        
	} // end main lhs loop

	restore
} // end event loop

/* prep reg data for graphs */
loc regsave_path output/event_study_regsave
foreach event in any_rhosp any_disab any_health3to5 any_rw wneg2 any_rlost_job  any_rcancr any_rstrok any_rcardiac any_rmemory {
	foreach lhs in khlp { // at_pk at_kp  {
        foreach samp in fkids mkids {
            use "`regsave_path'/`event'_`lhs'_`samp'.dta", clear
            cap drop t
            gen t = -3 if var=="b3m"
            replace t = -2 if var=="b2"
            replace t = 0 if var=="e0"
            replace t = 1 if var=="a1"
            replace t = 2 if var=="a2"
            replace t = 3 if var=="a3"
            drop if mi(t)

            set obs 7 
            replace t = -1 if t==.
            sort t

            foreach n of varlist coef stderr ci_* {
                replace `n' = 0 if t==-1
            }
            replace group = "`samp'" if t == -1
            cap drop event
            gen event = "`event'"
            save "`regsave_path'/`event'_`lhs'_`samp'.dta", replace
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////
/* Helping graphs by female and male kids  */
////////////////////////////////////////////////////////////////////////////////////////

loc regsave_path output/event_study_regsave
loc plot_path output/bykgender
loc lhs_vars khlp
loc event_vars any_rhosp any_disab any_health3to5 any_rw any_rlost_job  any_rcancr any_rstrok any_rcardiac any_rmemory

foreach l in `lhs_vars' {
    foreach e in `event_vars' {
         // load female kid model
        use "`regsave_path'/`e'_`l'_fkids.dta", clear
        // add on male results
        append using "`regsave_path'/`e'_`l'_mkids.dta"


        // rename for graphign and add -1 -- did this in batch above I thnik
        keep t coef ci_lower ci_upper group

        sort t
        reshape wide coef ci_lower ci_upper, i(t) j(group) string

        label var t "Waves Since Event Recorded"
        if "`e'" == "any_rhosp" {
            loc title "Any Hospitalization"
        } 
        else if "`e'" == "any_health3to5" {
            loc title "Poor Health"
        }
        else if "`e'" == "any_disab" {
            loc title "Disability Onset"
        } 
        else if "`e'" == "fem_rw" {
            loc title "Female Widowed"
        }
        else if "`e'" == "any_c" {
            loc title "Cancer"
        }
        else if "`e'" == "any_s" {
            loc title "Stroke"
        }
        else if "`e'" == "any_card" {
            loc title "Cardiac"
        }
        else if "`e'" == "any_m" {
            loc title "Memory"
        }
        else {
            loc title "Effect of `e' on `lhs'"
        }

        // manually construct plot using each coef column
        twoway line coeffkids t, lcolor(black) graphregion(color(white)) || ///
        line coefmkids t, lcolor(black) lpattern(dash)  title("`title'", size(vlarge)) xlabel(-3(1)3, labsize(vlarge)) ylabel(-.05(.05).05, labsize(vlarge)) yline(0, lcolor(gs12)) xline(-0.5, lcolor(gs12) lpattern(dash)) legend(order(1 "Female Kids" 2 "Male Kids")) 
        // save
        graph export "`plot_path'/`e'_`l'_bykgender.pdf", replace

    }
}


